package com.xdjmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xdjmall.dao.OrderItemDao;
import com.xdjmall.entity.OrderItem;
import com.xdjmall.util.DBUtils;
import com.xdjmall.util.QueryCondition;

public class OrderItemDaoImpl implements OrderItemDao {

	@Override
	public Integer insert(OrderItem t) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public Integer delete(Integer id) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public Integer update(OrderItem t) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public OrderItem selectById(Integer id) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public Integer selectCount() throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public List<OrderItem> selectByPage(QueryCondition condition) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public List<OrderItem> selectByOrderId(Long orderId) throws SQLException {
		// TODO 严
		String sql = "select order_item_id,order_id,goods_id,goods_name,goods_cover_img,selling_price,goods_count,create_time from order_item where order_id = ?";
		List<OrderItem> orderItems = new ArrayList<>();
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setLong(1, orderId);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				OrderItem oi = new OrderItem();

				oi.setOrderItemId(rs.getInt(1));
				oi.setOrderId(rs.getInt(2));
				oi.setGoodsId(rs.getInt(3));
				oi.setGoodsName(rs.getString(4));
				oi.setGoodsCoverImg(rs.getString(5));
				oi.setSellingPrice(rs.getBigDecimal(6));
				oi.setGoodsCount(rs.getInt(7));
				oi.setCreateTime(rs.getDate(8).toLocalDate());
				orderItems.add(oi);
			}
		}
		return orderItems.isEmpty() ? Collections.emptyList() : orderItems;
	}

	@Override
	public List<OrderItem> selectByOrderIds(List<Integer> orderIds) throws SQLException {
		// TODO 严
		String sql = "select order_item_id,order_id,goods_id,goods_name,goods_cover_img,selling_price,goods_count,create_time from order_item where order_id in";
		for (int i = 0; i < orderIds.size(); i++) {
			if (i == 0) {
				sql += "(?";
			} else if (i == orderIds.size() - 1) {
				sql += ",?)";
			} else {
				sql += ",?";
			}
		}
		List<OrderItem> orderItems = new ArrayList<>();
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			for (int i = 0; i < orderIds.size(); i++) {
				ps.setInt(i + 1, orderIds.get(i));
			}
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				OrderItem oi = new OrderItem();

				oi.setOrderItemId(rs.getInt(1));
				oi.setOrderId(rs.getInt(2));
				oi.setGoodsId(rs.getInt(3));
				oi.setGoodsName(rs.getString(4));
				oi.setGoodsCoverImg(rs.getString(5));
				oi.setSellingPrice(rs.getBigDecimal(6));
				oi.setGoodsCount(rs.getInt(7));
				oi.setCreateTime(rs.getDate(8).toLocalDate());
				orderItems.add(oi);
			}
		}
		return orderItems.isEmpty() ? Collections.emptyList() : orderItems;
	}

	@Override
	public Integer insertBatch(List<OrderItem> orderItems) throws SQLException {
		// TODO 严
		String sql = "insert into order_item(order_id,goods_id,goods_name,goods_cover_img,selling_price,goods_count) "
				+ " values(?,?,?,?,?,?)";
		int row = 0;
		try (Connection c = DBUtils.getConnection(); 
			PreparedStatement ps = c.prepareStatement(sql);) {
			
			for (int i = 0; i < orderItems.size(); i++) {
				ps.setLong(1, orderItems.get(i).getOrderId());
				ps.setLong(2, orderItems.get(i).getGoodsId());
				ps.setString(3, orderItems.get(i).getGoodsName());
				ps.setString(4, orderItems.get(i).getGoodsCoverImg());
				ps.setBigDecimal(5, orderItems.get(i).getSellingPrice());
				ps.setInt(6, orderItems.get(i).getGoodsCount());
				ps.addBatch();
			}
			int[] rows = ps.executeBatch();
			return rows.length;
		}
	}

}
